let editor = document.getElementById("editor");
editor.addEventListener("beforeinput", function (e){
  e.preventDefault();
  if (e.inputType == "insertText" && /[a-zA-Z]/.test(e.data)) {
    editor.innerHTML += e.data;
    setCurPosition(editor.childNodes[0], editor.innerHTML.length)
  } else if (e.inputType == "deleteContentBackward") {
    if (editor.innerHTML.length > 0) {
      editor.innerHTML = editor.innerHTML.slice(0, -1)
      setCurPosition(editor.childNodes[0], editor.innerHTML.length)
    }
  }
})


function setCurPosition (node, offset) {
  const selection = document.getSelection()
  const range = document.createRange()
  range.setStart(node, offset)
  range.setEnd(node, offset)
  selection.removeAllRanges()
  selection.addRange(range)
}

